
#!/bin/bash

# --- 辅助函数：将单个十进制数转换为256位掩码数组 ---
# 输入: 一个0-255的十进制数
# 输出: 无 (修改全局数组 single_mask_array)
# 返回: 0成功, 非0失败
generate_single_mask_array() {
    local decimal_input="$1"
    # 重置数组
    single_mask_array=()
    local i
    for ((i = 0; i < 8; i++)); do
        single_mask_array[i]="00000000"
    done

    if ! [[ "$decimal_input" =~ ^[0-9]+$ ]] || [ "$decimal_input" -lt 0 ] || [ "$decimal_input" -gt 255 ]; then
        echo "generate_single_mask_array 错误: '$decimal_input' 不是有效的 0-255 范围内的整数。" >&2
        return 1
    fi

    local BITS_PER_GROUP=32
    local group_index=$((decimal_input / BITS_PER_GROUP))
    local bit_position_in_group=$((decimal_input % BITS_PER_GROUP))
    local group_value_decimal=$((1 << bit_position_in_group))
    # 注意：组索引在数组中是从左到右 (0-7)，但位是从右数的组开始
    local array_index=$((7 - group_index))
    single_mask_array[$array_index]=$(printf "%08X" "$group_value_decimal")
    return 0
}

# --- 主函数：处理逗号分隔的输入列表 ---
# 功能：将逗号分隔的十进制数列表转换为合并的256位十六进制掩码
# 参数：$1 - 逗号分隔的十进制数列表 (e.g., "1,2,202")
# 输出：标准输出打印合并后的掩码字符串
# 返回：成功返回0，失败返回非0
decimal_to_256bit_hex_mask() {
    local input_list="$1"
    local -a single_mask_array=() # 用于存储单个掩码的数组
    local -a final_mask_array=()  # 用于存储最终合并掩码的数组

    # --- 参数验证 ---
    if [ $# -ne 1 ] || [ -z "$input_list" ]; then
        echo "错误 (函数): 请提供一个非空的逗号分隔的十进制数列表。" >&2
        return 1
    fi

    # 初始化最终掩码数组为全0
    local i
    for ((i = 0; i < 8; i++)); do
        final_mask_array[i]="00000000"
    done

    # --- 处理列表中的每个数字 ---
    IFS=',' read -ra numbers <<< "$input_list"
    local num
    for num in "${numbers[@]}"; do
        # 去除可能的前后空格
        num=$(echo "$num" | xargs)
        if [ -z "$num" ]; then
            continue # 跳过空项
        fi

        # 为当前数字生成掩码数组
        if ! generate_single_mask_array "$num"; then
            # 错误信息已在 generate_single_mask_array 中打印
            return 1
        fi

        # 将生成的单个掩码与最终掩码进行按位 OR 运算
        for ((i = 0; i < 8; i++)); do
            # Bash 的按位或运算符是 |
            # 需要先将十六进制字符串转换为十进制进行计算，再转回十六进制
            local final_dec=$(( 0x${final_mask_array[i]} ))
            local single_dec=$(( 0x${single_mask_array[i]} ))
            local result_dec=$(( final_dec | single_dec ))
            final_mask_array[i]=$(printf "%08X" "$result_dec")
        done
    done

    # --- 输出最终合并的掩码 ---
    local output_mask=""
    for ((i = 0; i < 8; i++)); do
        output_mask="${output_mask}${final_mask_array[i]}"
        if [ $i -lt 7 ]; then
            output_mask="${output_mask},"
        fi
    done

    echo "$output_mask"
    return 0
}

sudo echo nop    > /sys/kernel/debug/tracing/current_tracer
sudo echo 0      > /sys/kernel/debug/tracing/tracing_on
sudo echo 0      > /sys/kernel/debug/tracing/trace

# 系统软中断跟踪
sudo echo 1      > /sys/kernel/debug/tracing/events/irq/softirq_entry/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/irq/softirq_exit/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/irq/softirq_raise/enable

# 系统硬中断跟踪
sudo echo 1 >  /sys/kernel/debug/tracing/events/irq/irq_handler_entry/enable
sudo echo 1 >  /sys/kernel/debug/tracing/events/irq/irq_handler_exit/enable

#系统软中断堆栈跟踪
sudo echo 'stacktrace'   > /sys/kernel/debug/tracing/events/irq/softirq_entry/trigger

# 系统所有调用跟踪
sudo echo 1      > /sys/kernel/debug/tracing/events/raw_syscalls/sys_enter/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/raw_syscalls/sys_exit/enable

# 针对特定系统调用(Futex)的跟踪
sudo echo 1     > /sys/kernel/debug/tracing/events/syscalls/sys_enter_futex/enable
sudo echo 1     > /sys/kernel/debug/tracing/events/syscalls/sys_exit_futex/enable

# 任务调度跟踪
sudo echo 1      > /sys/kernel/debug/tracing/events/sched/sched_switch/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/sched/sched_waking/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/sched/sched_migrate_task/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/sched/sched_wakeup_new/enable

# 内核函数调用跟踪 "hal_kernel_svm_dam_desc_create"
sudo echo function_graph      > /sys/kernel/debug/tracing/current_tracer
sudo echo hal_kernel_svm_dam_desc_create      > /sys/kernel/debug/tracing/set_ftrace_filter

# 内存页表跟踪
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_wakeup_kswapd/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_begin/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_end/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_memcg_reclaim_begin/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_memcg_reclaim_end/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_node_reclaim_begin/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_node_reclaim_end/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_wake/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_sleep/enable

# 内存页表跟踪
sudo echo 1      > /sys/kernel/debug/tracing/events/compaction/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/compaction/mm_compaction_begin/enable
sudo echo 1      > /sys/kernel/debug/tracing/events/compaction/mm_compaction_end/enable


# 跟踪CPU范围 256C 机型
cpu_mask=$(decimal_to_256bit_hex_mask $1)

echo "Executing: echo $cpu_mask > /sys/kernel/debug_tracing/tracing_cpumask"

echo "$cpu_mask" > /sys/kernel/debug/tracing/tracing_cpumask

sudo echo 1      > /sys/kernel/debug/tracing/tracing_on

sudo sleep $2
sudo echo 0      > /sys/kernel/debug/tracing/tracing_on

sudo echo 0      > /sys/kernel/debug/tracing/events/irq/softirq_entry/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/irq/softirq_exit/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/irq/softirq_raise/enable

echo 0 >  /sys/kernel/debug/tracing/events/irq/irq_handler_entry/enable
echo 0 >  /sys/kernel/debug/tracing/events/irq/irq_handler_exit/enable

sudo echo 0      > /sys/kernel/debug/tracing/events/sched/sched_switch/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/sched/sched_waking/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/sched/sched_wakeup_new/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/sched/sched_migrate_task/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/raw_syscalls/sys_enter/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/raw_syscalls/sys_exit/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/syscalls/sys_enter_futex/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/syscalls/sys_exit_futex/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_wakeup_kswapd/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/compaction/mm_compaction_begin/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/compaction/mm_compaction_end/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_begin/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_end/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_memcg_reclaim_begin/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_memcg_reclaim_end/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_node_reclaim_begin/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_node_reclaim_end/enable

sudo echo 0      > /sys/kernel/debug/tracing/events/compaction/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_wake/enable
sudo echo 0      > /sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_sleep/enable

sudo echo 'traceoff'   > /sys/kernel/debug/tracing/events/irq/softirq_entry/trigger

sudo echo nop      > /sys/kernel/debug/tracing/current_tracer
sudo echo    > /sys/kernel/debug/tracing/set_ftrace_filter

sudo cat /sys/kernel/debug/tracing/trace  > ./irq_trace_$2


